import traceback


# 单进程内单例装饰器
def singleton(cls, *args, **kwargs):
    instances = {}

    def wrapper(*_, **__):
        if cls not in instances:
            instances[cls] = cls(*_, **__)
        return instances[cls]

    return wrapper


# 异常捕获
def capture_error(default_val=None):
    def handle(func):
        def do(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except:
                print(f"{func.__name__} error-->>{str(traceback.format_exc())}")
                return default_val

        return do

    return handle
